FUNCTION YBC_SWS_GET_APPROVER.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_WFCONFID) TYPE  YBC_SWS_WFCONFID
*"     VALUE(IV_APPLEVEL) TYPE  YBC_SWS_APPLEVEL OPTIONAL
*"     VALUE(IV_FIRST_LEVEL) TYPE  CHAR1 OPTIONAL
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"  TABLES
*"      IT_REQ_HEADER STRUCTURE  YBCS_SWS_FVALUE OPTIONAL
*"      ET_APPROVER STRUCTURE  YBCS_SWS_APPROVER
*"----------------------------------------------------------------------
  DATA:
    LS_TEMP     TYPE CHAR50,
    LT_TEMP     TYPE STANDARD TABLE OF CHAR50,
    LS_TAB      TYPE SWHACTOR,
    LT_TAB      TYPE STANDARD TABLE OF SWHACTOR,
    LS_WF_01    TYPE YBC_SWS_WF_01,
    LS_WF_01_T  TYPE YBC_SWS_WF_01,
    LS_WF_CND   TYPE YBC_SWS_WF_CND,
    LT_WF_01    TYPE STANDARD TABLE OF YBC_SWS_WF_01,
    LT_WF_CND   TYPE STANDARD TABLE OF YBC_SWS_WF_CND,
    LT_WF_CND_T TYPE STANDARD TABLE OF YBC_SWS_WF_CND.

  DATA:
    LV_INDEX     TYPE I,
    LV_CONDITION TYPE STRING,
    LV_JSON      TYPE STRING,
    LT_SELOPT    TYPE RSDS_SELOPT_T,
    LV_CHAR20    TYPE CHAR20,
    LV_S_NAME    TYPE CHAR20,
    LV_ANDOR     TYPE YBC_SWS_WF_CND-ANDOR,
    S_FIELD1     TYPE RSDS_SELOPT_T,
    S_FIELD2     TYPE RSDS_SELOPT_T,
    S_FIELD3     TYPE RSDS_SELOPT_T,
    S_FIELD4     TYPE RSDS_SELOPT_T,
    S_FIELD5     TYPE RSDS_SELOPT_T,
    S_FIELD6     TYPE RSDS_SELOPT_T,
    S_FIELD7     TYPE RSDS_SELOPT_T,
    S_FIELD8     TYPE RSDS_SELOPT_T,
    S_FIELD9     TYPE RSDS_SELOPT_T,
    S_FIELD10    TYPE RSDS_SELOPT_T.

  FIELD-SYMBOLS <FT_RANGE> TYPE ANY TABLE.

** Dynamic Table Declarations
  DATA : LT_DYN_TABLE TYPE REF TO DATA,
         LS_DYN_LINE  TYPE REF TO DATA.

  FIELD-SYMBOLS:
    <FS_VALUE> TYPE ANY,
    <FS_LINE>  TYPE ANY,
    <FT_TABLE> TYPE STANDARD TABLE.

  DATA:
    LV_VALUE        TYPE STRING,
    LR_STRUCT_TYP   TYPE REF TO  CL_ABAP_DATADESCR,
    LR_DYNTABLE_TYP TYPE REF TO  CL_ABAP_TABLEDESCR,
    LS_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT,
    LT_COMPONENT    TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

  REFRESH ET_APPROVER[].
  IF IV_APPLEVEL IS INITIAL AND IV_FIRST_LEVEL IS INITIAL.
    ES_MSG-TYPE = 'E'.
    MESSAGE S016(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  "Get config
  SELECT
    *
    INTO TABLE LT_WF_01
    FROM YBC_SWS_WF_01
   WHERE WFCONFID = IV_WFCONFID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S018(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID.
    RETURN.
  ENDIF.

  SORT LT_WF_01 BY APPLEVEL WFCNDID.

  IF IV_APPLEVEL IS NOT INITIAL.
    DELETE LT_WF_01 WHERE APPLEVEL <> IV_APPLEVEL.
    IF LT_WF_01 IS INITIAL.
      ES_MSG-TYPE = 'E'.
      MESSAGE S017(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID IV_APPLEVEL.
      RETURN.
    ENDIF.
  ENDIF.

  IF IV_FIRST_LEVEL IS NOT INITIAL.
    READ TABLE LT_WF_01 INTO LS_WF_01_T INDEX 1.
    DELETE LT_WF_01 WHERE APPLEVEL <> LS_WF_01_T-APPLEVEL.
  ENDIF.

  CLEAR LS_WF_01.
  "Get config condition
  SELECT *
    INTO TABLE LT_WF_CND
    FROM YBC_SWS_WF_CND
    FOR ALL ENTRIES IN LT_WF_01
   WHERE WFCONFID = LT_WF_01-WFCONFID
     AND WFCNDID = LT_WF_01-WFCNDID.
  IF SY-SUBRC <> 0.
    READ TABLE LT_WF_01 INTO LS_WF_01 INDEX 1.

  ELSE.
    "动态创建内表
    LOOP AT IT_REQ_HEADER.
      CLEAR LS_COMPONENT.
      "字段中不能含有&,否则会Dump
      IF IT_REQ_HEADER-FIELD CS '&'.
        REPLACE ALL OCCURRENCES OF '&' IN IT_REQ_HEADER-FIELD WITH '_'.
      ENDIF.
      LS_COMPONENT-NAME = IT_REQ_HEADER-FIELD.

      READ TABLE LT_WF_CND TRANSPORTING NO FIELDS
        WITH KEY FIELD = IT_REQ_HEADER-FIELD FIELDTYPE = '01'."数字或者金额类型
      IF SY-SUBRC = 0.
        LS_COMPONENT-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'DEC_16_04_S' ).
      ELSE.
        LS_COMPONENT-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'TEXT1000' ).
      ENDIF.
      APPEND LS_COMPONENT TO LT_COMPONENT.
    ENDLOOP.

    IF LT_COMPONENT[] IS NOT INITIAL.
      LR_STRUCT_TYP  ?= CL_ABAP_STRUCTDESCR=>CREATE( P_COMPONENTS = LT_COMPONENT ).
      LR_DYNTABLE_TYP = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LR_STRUCT_TYP ).

      CREATE DATA:
        LT_DYN_TABLE TYPE HANDLE LR_DYNTABLE_TYP,
        LS_DYN_LINE TYPE HANDLE LR_STRUCT_TYP.

      "生成动态内表
      ASSIGN LT_DYN_TABLE->* TO <FT_TABLE>.
      ASSIGN LS_DYN_LINE->* TO <FS_LINE>.

      LOOP AT IT_REQ_HEADER.
        ASSIGN COMPONENT IT_REQ_HEADER-FIELD OF STRUCTURE <FS_LINE> TO <FS_VALUE>.
        IF SY-SUBRC = 0.
          READ TABLE LT_WF_CND TRANSPORTING NO FIELDS
            WITH KEY FIELD = IT_REQ_HEADER-FIELD FIELDTYPE = '01'."数字或者金额类型
          IF SY-SUBRC = 0.
            REPLACE ALL OCCURRENCES OF ',' IN IT_REQ_HEADER-VALUE WITH ''.
          ENDIF.

          <FS_VALUE> = IT_REQ_HEADER-VALUE.
        ENDIF.
      ENDLOOP.

      IF <FS_LINE> IS NOT INITIAL.
        APPEND <FS_LINE> TO <FT_TABLE>.
      ENDIF.
    ENDIF.

    CLEAR:
      LS_WF_01.
    LOOP AT LT_WF_01 INTO LS_WF_01_T.
      IF LS_WF_01_T-WFCNDID IS INITIAL. "优先取不带条件ID的
        LS_WF_01 = LS_WF_01_T.
        EXIT.
      ELSE.
        LT_WF_CND_T = LT_WF_CND.
        DELETE LT_WF_CND_T WHERE WFCNDID <> LS_WF_01_T-WFCNDID.
        "拼接条件字符串
        CLEAR:
          LV_CONDITION,
          LV_INDEX.
        LOOP AT LT_WF_CND_T INTO LS_WF_CND.
          LV_ANDOR = LS_WF_CND-ANDOR.
          IF LV_ANDOR IS INITIAL.
            LV_ANDOR = 'AND'.
          ENDIF.

          AT LAST.
            CLEAR LV_ANDOR.
          ENDAT.

          LV_JSON = LS_WF_CND-CNDS.
          /UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_JSON
                                     CHANGING  DATA = LT_SELOPT[] ).

          LV_INDEX = LV_INDEX + 1.
          IF LV_INDEX > 10.
            EXIT.
          ENDIF.

          LV_S_NAME = 'S_FIELD' && LV_INDEX.
          ASSIGN (LV_S_NAME) TO <FT_RANGE>.
          <FT_RANGE> = LT_SELOPT[].

          CONCATENATE LV_CONDITION LS_WF_CND-FIELD 'IN' LV_S_NAME LV_ANDOR INTO LV_CONDITION SEPARATED BY SPACE.
        ENDLOOP.

        "找到符合条件的审批配置
        LOOP AT <FT_TABLE> INTO <FS_LINE> WHERE (LV_CONDITION).
          LS_WF_01 = LS_WF_01_T.
          EXIT.
        ENDLOOP.
        IF LS_WF_01 IS NOT INITIAL.
          EXIT.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.

  IF LS_WF_01 IS INITIAL.
    ES_MSG-TYPE = 'E'.
    MESSAGE S024(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID IV_APPLEVEL.
    RETURN.
  ENDIF.

  "Unit
  IF LS_WF_01-APPUNIT IS NOT INITIAL.
    REFRESH LT_TEMP.
    SPLIT LS_WF_01-APPUNIT AT ',' INTO TABLE LT_TEMP.
    LOOP AT LT_TEMP INTO LS_TEMP.
      CONDENSE LS_TEMP.
      REFRESH LT_TAB.
      CALL FUNCTION 'RH_STRUC_GET'
        EXPORTING
          ACT_OTYPE       = 'O'
          ACT_OBJID       = LS_TEMP
          ACT_WEGID       = 'OO-S-BP'
          ACT_BEGDA       = SY-DATUM
          ACT_ENDDA       = SY-DATUM
          AUTHORITY_CHECK = ' '
        TABLES
          RESULT_TAB      = LT_TAB
        EXCEPTIONS
          NO_PLVAR_FOUND  = 1
          NO_ENTRY_FOUND  = 2
          OTHERS          = 3.
      IF SY-SUBRC <> 0.
        CONTINUE.
      ENDIF.

      LOOP AT LT_TAB INTO LS_TAB WHERE OTYPE = 'US'.
        CLEAR ET_APPROVER.
        ET_APPROVER-APPLEVEL = LS_WF_01-APPLEVEL.
        ET_APPROVER-APPLEVEL_DESC1 = LS_WF_01-APPLEVEL_DESC1.
        ET_APPROVER-APPLEVEL_DESC2 = LS_WF_01-APPLEVEL_DESC2.
        ET_APPROVER-APPLEVEL_DESC3 = LS_WF_01-APPLEVEL_DESC3.
        ET_APPROVER-SAPID = LS_TAB-OBJID.
        ET_APPROVER-COUNTERSIGN = LS_WF_01-COUNTERSIGN.

        PERFORM FORM_GET_USER_DATA CHANGING ET_APPROVER.
        APPEND ET_APPROVER.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
  "
  "Position
  IF LS_WF_01-APPPOSITION IS NOT INITIAL.
    REFRESH LT_TEMP.
    SPLIT LS_WF_01-APPPOSITION AT ',' INTO TABLE LT_TEMP.
    LOOP AT LT_TEMP INTO LS_TEMP.
      CONDENSE LS_TEMP.

      REFRESH LT_TAB.
      CALL FUNCTION 'RH_STRUC_GET'
        EXPORTING
          ACT_OTYPE       = 'S'
          ACT_OBJID       = LS_TEMP
          ACT_WEGID       = 'OO-S-BP'
          ACT_BEGDA       = SY-DATUM
          ACT_ENDDA       = SY-DATUM
          AUTHORITY_CHECK = ' '
        TABLES
          RESULT_TAB      = LT_TAB
        EXCEPTIONS
          NO_PLVAR_FOUND  = 1
          NO_ENTRY_FOUND  = 2
          OTHERS          = 3.
      IF SY-SUBRC <> 0.
        CONTINUE.
      ENDIF.

      LOOP AT LT_TAB INTO LS_TAB WHERE OTYPE = 'US'.
        CLEAR ET_APPROVER.
        ET_APPROVER-APPLEVEL = LS_WF_01-APPLEVEL.
        ET_APPROVER-APPLEVEL_DESC1 = LS_WF_01-APPLEVEL_DESC1.
        ET_APPROVER-APPLEVEL_DESC2 = LS_WF_01-APPLEVEL_DESC2.
        ET_APPROVER-APPLEVEL_DESC3 = LS_WF_01-APPLEVEL_DESC3.
        ET_APPROVER-SAPID = LS_TAB-OBJID.
        ET_APPROVER-COUNTERSIGN = LS_WF_01-COUNTERSIGN.

        PERFORM FORM_GET_USER_DATA CHANGING ET_APPROVER.
        APPEND ET_APPROVER.
      ENDLOOP.
    ENDLOOP.
  ENDIF.

  "User
  IF LS_WF_01-APPUSER IS NOT INITIAL.
    SPLIT LS_WF_01-APPUSER AT ',' INTO TABLE LT_TEMP.
    LOOP AT LT_TEMP INTO LS_TEMP.
      CONDENSE LS_TEMP.
      CLEAR ET_APPROVER.
      ET_APPROVER-APPLEVEL = LS_WF_01-APPLEVEL.
      ET_APPROVER-APPLEVEL_DESC1 = LS_WF_01-APPLEVEL_DESC1.
      ET_APPROVER-APPLEVEL_DESC2 = LS_WF_01-APPLEVEL_DESC2.
      ET_APPROVER-APPLEVEL_DESC3 = LS_WF_01-APPLEVEL_DESC3.
      ET_APPROVER-SAPID = LS_TEMP.
      ET_APPROVER-EXTUSID = LS_TEMP.
      ET_APPROVER-COUNTERSIGN = LS_WF_01-COUNTERSIGN.

      PERFORM FORM_GET_USER_DATA CHANGING ET_APPROVER.
      APPEND ET_APPROVER.
    ENDLOOP.
  ENDIF.

  "Role
  IF LS_WF_01-APPROLE IS NOT INITIAL.
    SPLIT LS_WF_01-APPROLE AT ',' INTO TABLE LT_TEMP.
    LOOP AT LT_TEMP INTO LS_TEMP.
      CONDENSE LS_TEMP.
      CLEAR ET_APPROVER.
      ET_APPROVER-APPLEVEL = LS_WF_01-APPLEVEL.
      ET_APPROVER-APPLEVEL_DESC1 = LS_WF_01-APPLEVEL_DESC1.
      ET_APPROVER-APPLEVEL_DESC2 = LS_WF_01-APPLEVEL_DESC2.
      ET_APPROVER-APPLEVEL_DESC3 = LS_WF_01-APPLEVEL_DESC3.
      ET_APPROVER-COUNTERSIGN = LS_WF_01-COUNTERSIGN.
      " ET_APPROVER-APPLEVEL_LAST = LS_WF_01-APPLEVEL_LAST.

      SELECT *
        INTO TABLE @DATA(LT_ARG_USERS)
        FROM AGR_USERS
       WHERE AGR_NAME = @LS_TEMP
         AND FROM_DAT <= @SY-DATUM
         AND TO_DAT >= @SY-DATUM.

      LOOP AT LT_ARG_USERS INTO DATA(LS_ARG_USERS).
        ET_APPROVER-SAPID = LS_ARG_USERS-UNAME.
        PERFORM FORM_GET_USER_DATA CHANGING ET_APPROVER.

        APPEND ET_APPROVER.
      ENDLOOP.
    ENDLOOP.
  ENDIF.

  IF ET_APPROVER[] IS INITIAL.
    MESSAGE S017(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_WFCONFID IV_APPLEVEL.
    ES_MSG-TYPE = 'E'.
    RETURN.
  ENDIF.

  DATA:
    LT_AG LIKE TABLE OF ET_APPROVER WITH HEADER LINE.

  "删除账号非激活的，并处理代理
  SELECT *
    FROM YBC_SWS_USER
    INTO TABLE @DATA(LT_SWS_USER)
     FOR ALL ENTRIES IN @ET_APPROVER
   WHERE USERID = @ET_APPROVER-USERID.

  SORT LT_SWS_USER BY USERID.

  LOOP AT ET_APPROVER WHERE USERID IS NOT INITIAL.
    LV_INDEX = SY-TABIX.

    READ TABLE LT_SWS_USER INTO DATA(LS_SWS_USER) WITH KEY USERID = ET_APPROVER-USERID
      BINARY SEARCH.
    IF SY-SUBRC = 0.
      IF LS_SWS_USER-INACTIVE IS NOT INITIAL."该账号非激活则不发送
        DELETE ET_APPROVER INDEX LV_INDEX.
        CONTINUE.

      ELSE.
        "当前时间有代理人
        IF LS_SWS_USER-AG_USERID IS NOT INITIAL
          AND LS_SWS_USER-AG_BEGDA <= SY-DATUM AND LS_SWS_USER-AG_ENDDA >= SY-DATUM.

          CLEAR ET_APPROVER.
          ET_APPROVER-USERID = LS_SWS_USER-AG_USERID.

          APPEND ET_APPROVER TO LT_AG.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.

  IF LT_AG[] IS NOT INITIAL.
    SELECT *
      FROM YBC_SWS_USER
      INTO TABLE @DATA(LT_SWS_USER_AG)
       FOR ALL ENTRIES IN @LT_AG
     WHERE USERID = @LT_AG-USERID.

    LOOP AT LT_SWS_USER_AG INTO DATA(LS_SWS_USER_AG).
      MOVE-CORRESPONDING LS_SWS_USER_AG TO ET_APPROVER.
      APPEND ET_APPROVER.
    ENDLOOP.
  ENDIF.

  SORT ET_APPROVER BY EXTUSID SAPID.
  DELETE ADJACENT DUPLICATES FROM ET_APPROVER COMPARING EXTUSID SAPID.
ENDFUNCTION.
